import java.io.*;
import java.util.Scanner;

//字节的读写
public class Demo2 {
    //利用scanner进行字符读取
    public static void main(String[] args) {
        try(InputStream stream=new FileInputStream("D:/exercise/hello.txt")) {
         try(Scanner scan=new Scanner(stream,"UTF-8")) {
             while(scan.hasNext()){
                 String s=scan.next();
                 System.out.println(s);
             }
         }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    public static void main4(String[] args) {
        try (InputStream stream=new FileInputStream("D:/exercise/hello.txt")){
           //一个数组的读入
           byte [] buf=new byte[100];
           int len;
           while(true){
               len=stream.read(buf);
               if(len==-1){
                   break;
               }
               for (int i = 0; i <len ; i++) {
                   System.out.println(buf[i]);
               }
           }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    //文件内容读
    public static void main2(String[] args) {
        try(InputStream stream=new FileInputStream("D:/exercise/hello.txt")) {
            //一个字节一个的读
            while(true){
                int num=stream.read();
                if(num==-1){//读到-1说明已经读到文件末尾
                    break;
                }
                System.out.println(num);
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    //文件内容的写
    public static void main1(String[] args) {
        try (OutputStream outputStream = new FileOutputStream("D:/exercise/hello.txt")) {
            //一个一个的写
            outputStream.write(97);
            outputStream.write(98);
            //一个一个数组的写入
            byte[] ret = new byte[]{99, 100, 101};
            outputStream.write(ret);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
